home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: How do I modify a character string that's declared as an array of char?
- Date: Wed, 28 Feb 96 19:20:43 GMT
- Organization: none
- Message-ID: <825535243snz@genesis.demon.co.uk>
- References: <4gj2nl$840@mirzam.usc.edu> <4gjbjh$o0h@news-f.iadfw.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4gjbjh$o0h@news-f.iadfw.net>
- alpet@airmail.net "Adam Peterson" writes:
-
- >awawda@mirzam.usc.edu (Abu A. Wawda) wrote:
- >
- >>How do I write a function that can modify a string that was declared
- >>as an array of characters?
- >
- >>for example:
- >
- >>myfunction(char **s)
- >>{
- >> // modify string s
-
- Since you are posting to comp.lang.c post valid C code. // may introduce a
- comment in C++ but it is a syntax error in C.
-
- >>}
- >
- >>main()
- >>{
- >> char mydata[50];
- >
- >> strcpy(mydata,"test string");
- >> myfunction(&mydata);
- >>}
-
- myfunction takes a pointer to a pointer but this is trying to pass it a
- pointer to an array which is illegal in C (pointers and arrays are entirely
- different things). Your compiler should have generated a warning or error
- when compiling this.
-
- >>this does not seem to work! it works fine if i pass i declare mydata
- >>as a pointer to a character like this (or dynamically allocate the
- >>string using a pointer) and pass the address of the pointer:
- >
- >>the problem with this is i need to write a function that will modify a
- >>string created with an array of characters. i can't figure out how do
- >>to this. please help. thanks!
- >
- >void myfunction(char *s)
- >{
- > strcat(s,"more stuff"); // modify string s
- >}
- >
- >void main()
-
- In C main returns int. Writing void main() gives the compiler the freedom to
- do anything it likes with the rest of the code - the whole program ceases
- to have any meaning at all.
-
- >{
- > char mydata[50];
- >
- > strcpy(mydata,"test string");
- > printf(mydata);
- > myfunction(mydata);
- > printf(mydata);
- >}
- >
- >The above does what I think you want to do. When you call
- >'myfunction', the '&' was not neccessary by definition, then *in*
- >'myfunction', you were dropping two levels of redirection down.
-
- I think you meant 'indirection'!
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-